package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SOrderVinService;
import cn.huiyunche.base.service.mappers.SOrderVinMapper;
import cn.huiyunche.base.service.model.SOrderVin;
import cn.huiyunche.base.service.model.SOrderVinExample;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SOrderVinServiceImpl implements SOrderVinService {

    private static final Logger logger = LoggerFactory.getLogger(SOrderVinServiceImpl.class);

    @Autowired
    private SOrderVinMapper sOrderVinMapper;

    @Override
    public List<SOrderVin> getList(long customerOrderId, long orderDetailId) {
        logger.info("getList params : {}, {}", customerOrderId, orderDetailId);
        if (0 == customerOrderId) {
            logger.error("getList customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0！");
        }
        if (0 == orderDetailId) {
            logger.error("getList orderDetailId must not be 0");
            throw new IllegalArgumentException("客户订单详情id不能是0！");
        }

        SOrderVinExample example = new SOrderVinExample();
        example.createCriteria().andOrderIdEqualTo(customerOrderId).andDetailIdEqualTo(orderDetailId);
        List<SOrderVin> list = sOrderVinMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            logger.error("getList list is empty");
            throw new BusinessException("获取客户订单车架号信息失败！");
        }

        return list;
    }

    @Override
    public List<SOrderVin> getList(long customerOrderId) {
        logger.info("getList params : {}", customerOrderId);
        if (0 == customerOrderId) {
            logger.error("getList customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0！");
        }

        SOrderVinExample example = new SOrderVinExample();
        example.createCriteria().andOrderIdEqualTo(customerOrderId);
        List<SOrderVin> list = sOrderVinMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            logger.error("getList list is empty");
            throw new BusinessException("获取客户订单车架号信息失败！");
        }

        return list;
    }

    @Override
    public void deleteByCOrderId(long customerOrderId) {
        logger.info("deleteByCOrderId param : {}", customerOrderId);
        if (0 == customerOrderId) {
            logger.error("deleteByCOrderId customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0");
        }

        SOrderVinExample example = new SOrderVinExample();
        example.createCriteria().andOrderIdEqualTo(customerOrderId);
        sOrderVinMapper.deleteByExample(example);
    }


}
